package controller;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import interceptor.UserLoginJsonInterceptor;
import model.Product;
import model.Purchases;
import model.Type;
import model.User;
import service.IProductService;
import service.IPurchasesService;
import service.ITypeService;
import service.impl.ProductServiceImpl;
import service.impl.PurchasesServiceImpl;
import service.impl.TypeServiceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ykj on 16/1/11.
 */
public class ProductController extends BaseController {

    public void addDialog(){
        render("/admin/productAddDialog.jsp");
    }

    public void updateDialog() {
        Integer id = getParaToInt("id");
        //没有id参数则返回401
        if(id==null) renderError(401);
        IProductService productService = new ProductServiceImpl();
        Product product = (Product) productService.findById(id);
        setAttr("product",product);
        render("/admin/productUpdateDialog.jsp");
    }

    public void save() {
        Product product = getModel(Product.class);
        System.out.println(product.toJson());
        IProductService productService = new ProductServiceImpl();
        Product result = (Product) productService.save(product);
        int id = result != null ? result.getId() : -1;
        renderSaveResult(id, result);
    }

    public void update() {
        Product product = getModel(Product.class);
        IProductService productService = new ProductServiceImpl();
        Product result = (Product) productService.update(product);
        int id = result != null ? result.getId() : -1;
        renderSaveResult(id, result);
    }

    public void delete() {
        Integer id = getParaToInt("id");
        //没有id参数则返回401
        if(id==null) renderError(401);
        IProductService productService = new ProductServiceImpl();
        boolean result = productService.delete(id);
        renderDelResult(result);
    }

    public void getColumns() {
        List<String> fields = new ArrayList<String>() {{
            add("id");
            add("type_name");
            add("name");
            add("need_time");
            add("rate_return");
            add("total_money");
            add("already_money");
            add("guarantee_name");
            add("pact");
            add("create_time");
        }};
        List<String> titles = new ArrayList<String>() {{
            add("标识");
            add("类型名称");
            add("产品名称");
            add("平均时长");
            add("年化收益率");
            add("募资总额");
            add("已募资金");
            add("担保机构名称");
            add("产品合同");
            add("创建日期");
        }};
        renderColumns(fields, titles);
    }

    public void products() {
        IProductService productService = new ProductServiceImpl();
        renderData(productService);
    }

    public void detail(){
        Integer id = getParaToInt("id");
        //没有id参数则返回401
        if(id==null) renderError(401);
        IProductService productService = new ProductServiceImpl();
        Product product = (Product) productService.findById(id);
        setAttr("product",product);
        render("/front/product.jsp");
    }

    //增加拦截器拦截未登录
    @Before(UserLoginJsonInterceptor.class)
    public void buy(){
        Integer productId = getParaToInt("productId");
        Integer invest = getParaToInt("invest");
        User user = (User) getSession().getAttribute("user");
        if(productId==null || invest==null || user==null){
            renderError(401);
            return;
        }
        IPurchasesService purchasesService = new PurchasesServiceImpl();
        Purchases buyPurchase = new Purchases();
        buyPurchase.setInvest(invest.doubleValue());
        buyPurchase.setProductId(productId);
        buyPurchase.setUserId(user.getId());

        Purchases result = (Purchases) purchasesService.save(buyPurchase);
        Map map = new HashMap();
        if(result!=null){
            map.put("success",true);
            map.put("message","购买成功,已购买"+result.getInvest()+"元");
        }else {
            map.put("success",false);
            map.put("message","购买失败,请确认您资金充足");
        }
        renderJson(map);
    }

    public void filterProduct(){
        IProductService productService = new ProductServiceImpl();
        String pageStr = getPara("page");
        String rowsStr = getPara("rows");
        int page = pageStr!=null?Integer.parseInt(pageStr):1;
        int rows = rowsStr!=null?Integer.parseInt(rowsStr):6;

        String rateSpan = getPara("rateSpan");
        String timeSpan = getPara("timeSpan");
        String type = getPara("type");
        System.out.println("rateSpan:"+rateSpan);
        System.out.println("timeSpan:"+timeSpan);
        System.out.println("type:"+type);

        Page<Product> products = productService.filterProducts(page, rows, rateSpan, timeSpan, type);
        removeAttr("products");
        setAttr("products", products);
        render("/front/productList.jsp");
    }
}
